package com.service.impl;

import com.constant.Constant;
import com.dao.GoldPointMapper;
import com.entity.GoldPoints;
import com.entity.User;
import com.exception.DataAccessException;
import com.exception.ServiceException;
import com.github.pagehelper.PageHelper;
import com.service.GoldPointService;
import com.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import com.dao.GoldPointMapper;
import com.entity.GoldPoints;
import com.github.pagehelper.PageInfo;
import com.service.GoldPointService;
import com.vo.GoldPointsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class GoldPointServiceImpl implements GoldPointService {
    @Autowired
    GoldPointMapper goldPointMapper;

    //@李奇洲
    @Override
    public int insertPointGoldCount(GoldPoints goldPoints) {
        return goldPointMapper.insertPointGoldCount(goldPoints);
    }



    public PageInfo findAllRecords(int pageNo,int id){
        PageHelper.startPage( pageNo, Constant.PAGE_SIZE);
        PageInfo<GoldPoints> info= new PageInfo<GoldPoints>();
        List list= goldPointMapper.findAllRecords(id);
        if(list!=null){
           info.setList(list);
        }else{
            throw new ServiceException("查询失败");
        }

       return info;
    }
    public GoldPointsVo findGoldPointCount(int id){
        GoldPointsVo goldPointsVo=goldPointMapper.findGoldPointCount(id);
        return goldPointsVo;
    }
    public int exchangePoints(int id ,Integer pointsCt){
        int i = goldPointMapper.exchangePoints(id, pointsCt);
        return i;

    }

    @Override
    public AjaxResult findFrontSumByUid(int id) {
        AjaxResult result=new AjaxResult(false,"查询失败",null);

        GoldPointsVo frontSumByUid = goldPointMapper.findFrontSumByUid(id);
        if(frontSumByUid==null){
            throw new ServiceException("查询失败");

        }else{
            result.setObj(frontSumByUid );
        }
        return result;


    }
//杨跃森
    @Override
    public int AddPointGold(GoldPoints gp) {
        int i=0;
        try {
            i = goldPointMapper.insertPointGold(gp);
        }catch (ServiceException se){
            se.printStackTrace();
            throw new DataAccessException("异常");
        }
        if (i!=1){
            throw new SecurityException("签到失败");
        }
        return i;
    }
//有问题
    @Override
    public Date findPointGold(Integer id) {
        Date date = null;
        try {
            date = goldPointMapper.selectPointGold(id);
        }catch (ServiceException se){
            se.printStackTrace();
            throw new DataAccessException("查询最后一次记录失败");
        }
        return date;
    }

}
